<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/11/14
 * Time: 20:58
 */
class core_pdd extends L {

    /**
     * 描述 ：新增或修改订单
     * 参数 ：$params {
     *      PDD完整订单信息
     * }
     * $account : 店铺名称  修改时不需要传，新增必填
     * 作者 ：Zoro.Zhu
    **/
    public static function setOrder($params, $account = ''){
        if(core_base::trans()) {
            if(empty($params['order_sn']) || empty($params['confirm_time'])){
                $result = array('state' => 300, 'info' => '重要参数缺失');
            }else{
                $json = addslashes(json_encode($params));
                $sql = "SELECT 
                    `pddOrder` 
                FROM 
                    `pdd_order` 
                WHERE 
                    `pddOrder` = '{$params['order_sn']}'
                FOR UPDATE";
                if($order = L::sql($sql)){
                    //订单已存在，修改
                    L::sql("UPDATE 
                        `pdd_order` 
                    SET 
                        `data` = '{$json}' 
                    WHERE 
                        `pddOrder` = '{$params['order_sn']}'");
                }else{
                    //新订单
                    if(empty($account)){
                        $result = array('state' => 301, 'info' => '新增订单必填账号');
                    }else{
                        $sql = "INSERT INTO `pdd_order`(
                            `pddOrder`, `confirmTime`, `data`, `addTime`, `orderState`, `accountPdd`
                        ) VALUES (
                            '{$params['order_sn']}', '{$params['confirm_time']}', '{$json}', '" . date('Y-m-d H:i:s') . "', 10, '{$account}')";
                        L::sql($sql);
                        //加入去阿里巴巴下单的消息队列
                        of_base_com_mq::set(['payAliOrder', $params['order_sn']], ['pddOrder' => $params['order_sn']]);
                    }

                }
            }
        }else{
            $result = array('state' => 500, 'info' => '未开启事务');
        }
        empty($result) && $result = array('state' => 200, 'info' => '操作正常');
        core_base::trans($result);
        return $result;
    }


}